# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head:
            return None
        first=head
        second=head.next
        if not second:
            return head
        newhead=head.next
        while True:
            temp=second.next
            second.next=first
            if temp and temp.next:
                first.next=temp.next
                first=temp
                second=temp.next
            elif temp and not temp.next:
                
                first.next=temp
                return newhead
            else:
                first.next=None
                return newhead